package com.app.manage.moudles.confs.controller;

import com.app.manage.moudles.confs.constants.VariTypeEnums;
import com.app.manage.moudles.confs.entity.TVari;
import com.app.manage.moudles.confs.service.TVariService;
import com.app.manage.utils.PageSearch;
import com.app.manage.utils.Rsp;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("vari")
public class TVariController {

    @Resource
    private TVariService tVariService;


    @GetMapping("listVariTyes")
    public Rsp listVariTyes(){
        List<Map<String,Object>> retList = new ArrayList<>( );
        for(VariTypeEnums  v : VariTypeEnums.values()){
            Map<String,Object> map = new HashMap<>();
            map.put("name",v.getName());
            map.put("value",v.getType());
            retList.add(map);
        }
        return Rsp.success(retList);
    }

    @PostMapping("pageList")
    public Rsp pageList(@RequestBody PageSearch<TVari> s){
        LambdaQueryWrapper<TVari> query = new LambdaQueryWrapper<>();
        query.like(StringUtils.isNotEmpty(s.getDomain().getVariName()),TVari::getVariName,s.getDomain().getVariName());
        query.eq(s.getDomain().getVariType() != null,TVari::getVariType,s.getDomain().getVariType());
        query.orderByDesc(TVari::getCreateTime);
        IPage<TVari>  res = tVariService.page(Page.of(s.getPageNum(),s.getPageSize()),query);
        //匹配字典
        Map<Integer,String> dictMap = VariTypeEnums.map();
        res.getRecords().forEach(o->{
            o.setVariTypeName(dictMap.get(o.getVariType()));
        });

        return Rsp.success(res);
    }

    @PostMapping("saveData")
    public Rsp saveData(@RequestBody TVari tVari){
        String res = tVariService.saveData(tVari);
        if(StringUtils.isNotEmpty(res)){
            return Rsp.fail(res);
        }
        return Rsp.success("保存完成！");
    }

    @GetMapping("getById")
    public Rsp getById(Integer id){
        return Rsp.success(tVariService.getById(id));
    }

}
